package com.mchsdk.paysdk.http.request;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.entity.GamePayTypeEntity;
import com.mchsdk.paysdk.http.RequestUtil;
import com.mchsdk.paysdk.utils.MCErrorCodeUtils;
import com.mchsdk.paysdk.utils.MCLog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhujinzhujin
 * on 2017/1/11.
 */

public class PayTypeRequest {

    private static final String TAG = "PayTypeRequest";

    HttpUtils http;
    Handler mHandler;

    public PayTypeRequest(Handler mHandler) {
        http = new HttpUtils();
        if (null != mHandler) {
            this.mHandler = mHandler;
        }
    }

    public void post(String url, RequestParams params) {
        if (TextUtils.isEmpty(url) || null == params) {
            MCLog.e(TAG, "fun#post url is null add params is null");
            noticeResult(Constant.GAME_PAY_TYPE_FAIL, "参数异常");
            return;
        }
        MCLog.w(TAG, "fun#post url = " + url);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = RequestUtil.getResponse( responseInfo);
                int status ;
                String msg = "";

                MCLog.w(TAG,"可选支付方式:"+response);

                String isHaveZFB = "0", isHaveWX = "0", isHaveJBY = "0", isHaveHFB = "0", isHaveJFT = "0";

                try {
                    JSONObject json = new JSONObject(response);
                    status = json.optInt("status");
                    if (status==200||status==1) {
                        isHaveZFB = json.optString("zfb_game","0");
                        isHaveWX = json.optString("wx_game","0");
                        isHaveJBY = json.optString("jby_game","0");
                        isHaveHFB = json.optString("hfb_game","0");
                        isHaveJFT = json.optString("jft_game","0");
                    } else {
                        if (!TextUtils.isEmpty(json.optString("return_msg"))) {
                            msg = json.optString("return_msg");
                        } else {
                            msg = MCErrorCodeUtils.getErrorMsg(status);
                        }
                        MCLog.e(TAG, "msg:" + msg);
                    }
                } catch (JSONException e) {
                    status = -1;
                } catch (Exception e) {
                    status = -1;
                }

                if (status==200||status==1){
                    GamePayTypeEntity gamePayTypeEntity = new GamePayTypeEntity();
                    gamePayTypeEntity.setHaveZFB("1".equals(isHaveZFB));
                    gamePayTypeEntity.setHaveWX("1".equals(isHaveWX));
                    gamePayTypeEntity.setHaveJBY("1".equals(isHaveJBY));
                    gamePayTypeEntity.setHaveHFB("1".equals(isHaveHFB));
                    gamePayTypeEntity.setHaveJFT("1".equals(isHaveJFT));

                    noticeResult(Constant.GAME_PAY_TYPE_SUCCESS, gamePayTypeEntity);
                } else {
                    if(TextUtils.isEmpty(msg)){
                        msg = "服务器异常";
                    }
                    noticeResult(Constant.GAME_PAY_TYPE_FAIL, msg);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                MCLog.e(TAG, "onFailure" + msg);
                noticeResult(Constant.GAME_PAY_TYPE_FAIL, "网络异常");
            }
        });
    }

    private void noticeResult(int type, Object object) {
        Message msg = new Message();
        msg.what = type;
        msg.obj = object;
        if (null != mHandler) {
            mHandler.sendMessage(msg);
        }
    }

}
